﻿<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:if test="Schema/@IsDropCode='1'">
if exists (select * from dbo.sysobjects where id = object_id(N'[_<xsl:value-of select="Schema/@TableName" />SelectAllPaged]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [_<xsl:value-of select="Schema/@TableName" />SelectAllPaged]
GO
</xsl:if>
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
--获取表<xsl:value-of select="Schema/@TableName" />的所有记录。请勿修改此存储过程，如果需要特殊的选择方法，请另写存储过程。
CREATE PROCEDURE _<xsl:value-of select="Schema/@TableName" />SelectAllPaged
	@PageSize int,
	@CurrentPage int output,
	@TotalPage int output,
	@TotalRecord int output,
	@SortBy varchar(100)
<xsl:if test="Schema/@IsEncrypt='1'">
	WITH ENCRYPTION
	</xsl:if>
AS

SET @SortBy=RTRIM(LTRIM(Replace(@SortBy,'''','''''')))
SELECT @TotalRecord=COUNT(*) FROM [<xsl:value-of select="Schema/@Owner" />].[<xsl:value-of select="Schema/@TableName" />]
SET @TotalPage=CEILING(CAST(@TotalRecord AS DECIMAL)/@PageSize)

IF @CurrentPage &lt; 0
	SET @CurrentPage=0
IF @CurrentPage &gt; @TotalPage-1
	SET @CurrentPage=@TotalPage-1

DECLARE @_Start INT, @_End INT
SET @_Start = ((@CurrentPage * @PageSize) + 1)
SET @_End = (@_Start + @PageSize - 1)

SELECT * FROM
(
SELECT *,ROW_NUMBER() OVER (ORDER BY @SortBy) AS RowNumber FROM [<xsl:value-of select="Schema/@Owner" />].[<xsl:value-of select="Schema/@TableName" />] <xsl:if test="Schema/@IsNoLock='1'">  WITH(NOLock)</xsl:if>
) AS TempTable
WHERE (RowNumber &gt;= @_Start) AND (RowNumber &lt;= @_End) 

  GO
  SET QUOTED_IDENTIFIER OFF
  GO
  SET ANSI_NULLS OFF
  GO
</xsl:template>
</xsl:stylesheet>